package ai.people.platform.archives.service.impl;

import ai.people.core.feign.api.system.SystemFeignClient;
import ai.people.netmon.framework.domain.archives.entity.ArchivesUserAssociation;
import ai.people.netmon.framework.domain.system.vo.NmSystemUserSimpleVO;
import ai.people.netmon.framework.exception.enums.ArchivesExceptionEnum;
import ai.people.netmon.framework.exception.enums.CommonEnum;
import ai.people.netmon.framework.model.response.ListResponse;
import ai.people.netmon.framework.model.response.Result;
import ai.people.netmon.framework.utils.AssertUtils;
import ai.people.platform.archives.service.ArchivesService;
import ai.people.platform.archives.service.ArchivesShareUserService;
import ai.people.platform.archives.service.ArchivesUserAssociationService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 共享用户服务impl
 *
 * @author yuanqinglong
 * @date 2022/3/31 15:25
 */
@Service
@RequiredArgsConstructor
public class ArchivesShareUserServiceImpl implements ArchivesShareUserService {

    private final ArchivesService archivesService;

    private final SystemFeignClient systemFeignClient;

    private final ArchivesUserAssociationService archivesUserAssociationServiceImpl;


    /**
     * 分享用户列表
     *
     * @return {@link List}<{@link NmSystemUserSimpleVO}>
     * @param archiveId 档案id
     */
    @Override
    public ListResponse<NmSystemUserSimpleVO> shareUserList(Long archiveId) {
        Result<ListResponse<NmSystemUserSimpleVO>> result = systemFeignClient.nonAdminUsers();
        ListResponse<NmSystemUserSimpleVO> listResponse = result.getData();
        List<Long> userIds = listResponse.getList().stream().map(NmSystemUserSimpleVO::getId).collect(Collectors.toList());
        if (userIds.isEmpty()){
            return listResponse;
        }
        // 查询此档案已分享的用户 标记为已分享
        List<ArchivesUserAssociation> associationList = archivesUserAssociationServiceImpl.listAllByUserIds(archiveId);
        Set<Long> userSets = associationList.stream().map(ArchivesUserAssociation::getUserId).collect(Collectors.toSet());
        listResponse.getList().forEach(nmSystemUserSimpleVO -> nmSystemUserSimpleVO.setShared(userSets.contains(nmSystemUserSimpleVO.getId())));
        return listResponse;
    }



    /**
     * 分享档案
     *
     * @param archivesUserAssociation 档案用户关系
     */
    @Override
    public void shareArchives(ArchivesUserAssociation archivesUserAssociation) {
        boolean archivesExists = archivesService.checkArchivesIsExists(archivesUserAssociation.getArchivesId());
        AssertUtils.isTrue(archivesExists,ArchivesExceptionEnum.ARCHIVES_DOES_NOT_EXIST);
        LambdaQueryWrapper<ArchivesUserAssociation> lambdaQuery = Wrappers.lambdaQuery(ArchivesUserAssociation.class);
        lambdaQuery.eq(ArchivesUserAssociation::getArchivesId,archivesUserAssociation.getArchivesId())
                .eq(ArchivesUserAssociation::getUserId,archivesUserAssociation.getUserId());
        ArchivesUserAssociation userAssociation = archivesUserAssociationServiceImpl.getOne(lambdaQuery);
        AssertUtils.isNull(userAssociation, ArchivesExceptionEnum.ARCHIVES_SHARED);
        boolean saveResult = archivesUserAssociationServiceImpl.save(archivesUserAssociation);
        AssertUtils.isTrue(saveResult, CommonEnum.FAIL);
    }

}
